package com.ruoyi.fjtseac.service

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.ruoyi.common.constant.HttpStatus
import com.ruoyi.common.core.redis.RedisCache
import com.ruoyi.fjtseac.domain.dto.EssayDto
import com.ruoyi.fjtseac.domain.po.Essay
import com.ruoyi.fjtseac.domain.po.Photo
import com.ruoyi.fjtseac.domain.po.Type
import com.ruoyi.fjtseac.domain.vo.EssayVo
import com.ruoyi.fjtseac.mapper.EssayMapper
import com.ruoyi.fjtseac.mapper.PhotoMapper
import com.ruoyi.fjtseac.mapper.TypeMapper
import com.ruoyi.fjtseac.service.impl.EssayServiceImpl
import com.ruoyi.fjtseac.util.FileUtil
import com.ruoyi.fjtseac.util.PoToVoMapper
import spock.lang.Specification
import spock.lang.Unroll

/**
 *
 * @author jxl
 * @date 2022/8/15
 */
class EssayServiceTest extends Specification {
    def essayMapper = Mock(EssayMapper);
    def fileUtil = Mock(FileUtil);
    def typeMapper = Mock(TypeMapper);
    def photoMapper = Mock(PhotoMapper);
    def redisCache = Mock(RedisCache);
    def essayServiceImp = new EssayServiceImpl(essayMapper, fileUtil, typeMapper, photoMapper, redisCache);

    @Unroll
    def "addEssay(essayDto:#essayDto), code:#httpStatus, msg:#resultMsg"() {
        given: "设置请求参数"
        def type = new Type(typeId: 7, typeName: "新世界");
        def type1 = null

        typeMapper.selectById(7) >> type
        typeMapper.selectById(100) >> type1

        essayMapper.insert(_ as Essay) >> 1

        when:
        def response = essayServiceImp.addEssay(essayDto);

        then:
        with(response) {
            code == httpStatus
            msg == resultMsg
        }

        where:
        essayDto                                                                                                                                                                                                                 || httpStatus         | resultMsg
        new EssayDto(title: "新闻资讯", content: "<p>你好世界</p>", coverUrl: "https://fjut-tiny-spark-1311549442.cos.ap-guangzhou.myqcloud.com/image/f104dd3e-9b81-424e-9999-949f85bb849e_ku.90sjimg.jpeg", typeId: 7, isCarousel: 0)   || HttpStatus.SUCCESS | "上传成功"
        new EssayDto(title: "新闻资讯", content: "<p>你好世界</p>", coverUrl: "https://fjut-tiny-spark-1311549442.cos.ap-guangzhou.myqcloud.com/image/f104dd3e-9b81-424e-9999-949f85bb849e_ku.90sjimg.jpeg", typeId: 100, isCarousel: 0) || HttpStatus.SUCCESS | "上传成功"

    }

    @Unroll
    def "selectById(essayId:#essayId), code:#httpStatus, msg:#essayVo"() {
        given: "设置请求参数"
        def essay = new Essay(essayId: 1, title: "新闻资讯", content: "<p>你好世界</p>", coverUrl: "https://fjut-tiny-spark-1311549442.cos.ap-guangzhou.myqcloud.com/image/f104dd3e-9b81-424e-9999-949f85bb849e_ku.90sjimg.jpeg", typeId: 7, isCarousel: 0);
        essayMapper.selectById(1) >> essay

        when:
        def response = essayServiceImp.selectById(1);

        then:
        with(response) {
            response.get("code") == httpStatus;
            response.get("data") == essayVo;
        }

        where:
        id || httpStatus         | essayVo
        1  || HttpStatus.SUCCESS | new EssayVo(essayId: 1, title: "新闻资讯", content: "<p>你好世界</p>", coverUrl: "https://fjut-tiny-spark-1311549442.cos.ap-guangzhou.myqcloud.com/image/f104dd3e-9b81-424e-9999-949f85bb849e_ku.90sjimg.jpeg", typeId: 7, isCarousel: 0)
    }

    @Unroll
    def "selectAll(pageNum:#pageNum, pageSize:#pageSize), code:#httpStatus"() {
        given: "设置请求参数"
        essayMapper.selectList(_ as QueryWrapper) >> resultSet

        when:
        def response = essayServiceImp.selectAll(pageNum, pageSize);

        then:
        with(response) {
            response.get("code") == httpStatus;
            response.get("data").getAt("total") == total
        }

        where:
        pageNum | pageSize | resultSet                                                                                                                                                                                                                                                                                                                                                                                                                                                  || httpStatus         | total
        1       | 2        | [new Essay(title: "新闻资讯", content: "<p>你好世界</p>", coverUrl: "https://fjut-tiny-spark-1311549442.cos.ap-guangzhou.myqcloud.com/image/f104dd3e-9b81-424e-9999-949f85bb849e_ku.90sjimg.jpeg", typeId: 7, isCarousel: 0, pv: 0), new Essay(title: "新闻资讯", content: "<p>你好世界</p>", coverUrl: "https://fjut-tiny-spark-1311549442.cos.ap-guangzhou.myqcloud.com/image/f104dd3e-9b81-424e-9999-949f85bb849e_ku.90sjimg.jpeg", typeId: 100, isCarousel: 0, pv: 0)] || HttpStatus.SUCCESS | 2
    }

    @Unroll
    def "selectEssayByType(typeId:#typeId, pageNum:#pageNum, pageSize:#pageSize), code:#httpStatus"() {
        given: "设置请求参数"
        essayMapper.selectList(_ as QueryWrapper) >> resultSet

        when:
        def response = essayServiceImp.selectEssayByType(typeId, pageNum, pageSize);

        then:
        with(response) {
            response.get("code") == httpStatus;
            response.get("data").getAt("total") == total
        }

        where:
        typeId | pageNum | pageSize | resultSet                                                                                                                                                                                                                                                                                                                                                                                                                                                || httpStatus         | total
        7      | 1       | 2        | [new Essay(title: "新闻资讯", content: "<p>你好世界</p>", coverUrl: "https://fjut-tiny-spark-1311549442.cos.ap-guangzhou.myqcloud.com/image/f104dd3e-9b81-424e-9999-949f85bb849e_ku.90sjimg.jpeg", typeId: 7, isCarousel: 0, pv: 0), new Essay(title: "新闻资讯", content: "<p>你好世界</p>", coverUrl: "https://fjut-tiny-spark-1311549442.cos.ap-guangzhou.myqcloud.com/image/f104dd3e-9b81-424e-9999-949f85bb849e_ku.90sjimg.jpeg", typeId: 7, isCarousel: 0, pv: 0)] || HttpStatus.SUCCESS | 2
    }

    @Unroll
    def "deleteEssayById(essayId:#essayId), code:#httpStatus, msg:#resultMsg"() {
        given: "设置请求参数"
        def host = "https://fjut-tiny-spark-1311549442.cos.ap-guangzhou.myqcloud.com"
        def photo = new Photo(essayId: 1, photoUrl: "image/507e08d3-a65f-44c8-b4d8-5224749fca64_ku.90sjimg-1.jpeg");

        essayMapper.deleteById(id) >> 1
        photoMapper.selectOne(_ as QueryWrapper) >> photo
        photoMapper.delete(_ as QueryWrapper) >> 1
        fileUtil.delete(photo.getPhotoUrl().replaceAll(host + "/", "")) >> true

        when:
        def response = essayServiceImp.deleteEssayById(id);

        then:
        with(response) {
            code == httpStatus
            msg == resultMsg
        }

        where:
        id || httpStatus         | resultMsg
        1  || HttpStatus.SUCCESS | "删除成功"
    }

    @Unroll
    def "updateEssay(essayDto:#essayDto), code:#httpStatus, msg:#resultMsg"() {
        given: "设置请求参数"
        def type = new Type(typeId: 7, typeName: "新世界");
        def type1 = null

        typeMapper.selectById(7) >> type
        typeMapper.selectById(100) >> type1

        essayMapper.updateById(_ as Essay) >> 1

        when:
        def response = essayServiceImp.updateEssay(essayDto);

        then:
        with(response) {
            code == httpStatus
            msg == resultMsg
        }

        where:
        essayDto                                                                                                                                                                                                                           || httpStatus         | resultMsg
        new EssayDto(essayId: 7, title: "新闻资讯", content: "<p>你好世界</p>", coverUrl: "https://fjut-tiny-spark-1311549442.cos.ap-guangzhou.myqcloud.com/image/f104dd3e-9b81-424e-9999-949f85bb849e_ku.90sjimg.jpeg", typeId: 7, isCarousel: 0) || HttpStatus.SUCCESS | "修改成功"
    }

    @Unroll
    def "selectEssayByTime(startTime:#startTime, endTime:#endTime, typeId:#typeId, pageNum:#pageNum, pageSize:#pageSize), code:#httpStatus"() {
        given: "设置请求参数"
        essayMapper.selectEssayByTime(startTime, endTime, typeId) >> resultSet

        when:
        def response = essayServiceImp.selectEssayByTime(startTime, endTime, typeId, pageNum, pageSize);

        then:
        with(response) {
            response.get("code") == httpStatus;
            response.get("data").getAt("total") == total
        }

        where:
        startTime    | endTime      | typeId | pageNum | pageSize | resultSet                                                                                                                                                                                                                                                                                                                                                                                                                                                || httpStatus         | total
        "2022-02-02" | "2022-07-07" | 7      | 1       | 2        | [new Essay(title: "新闻资讯", content: "<p>你好世界</p>", coverUrl: "https://fjut-tiny-spark-1311549442.cos.ap-guangzhou.myqcloud.com/image/f104dd3e-9b81-424e-9999-949f85bb849e_ku.90sjimg.jpeg", typeId: 7, isCarousel: 0, pv: 0), new Essay(title: "新闻资讯", content: "<p>你好世界</p>", coverUrl: "https://fjut-tiny-spark-1311549442.cos.ap-guangzhou.myqcloud.com/image/f104dd3e-9b81-424e-9999-949f85bb849e_ku.90sjimg.jpeg", typeId: 7, isCarousel: 0, pv: 0)] || HttpStatus.SUCCESS | 2
    }

    @Unroll
    def "selectEssayByTitle(title:#title, pageNum:#pageNum, pageSize:#pageSize), code:#httpStatus"() {
        given: "设置请求参数"
        essayMapper.selectList(_ as QueryWrapper) >> resultSet

        when:
        def response = essayServiceImp.selectEssayByTitle(title, pageNum, pageSize);

        then:
        with(response) {
            response.get("code") == httpStatus;
            response.get("data").getAt("total") == total
        }

        where:
        title  | pageNum | pageSize | resultSet                                                                                                                                                                                                                                                                                                                                                                                                                                                || httpStatus         | total
        "新闻资讯" | 1       | 2        | [new Essay(title: "新闻资讯", content: "<p>你好世界</p>", coverUrl: "https://fjut-tiny-spark-1311549442.cos.ap-guangzhou.myqcloud.com/image/f104dd3e-9b81-424e-9999-949f85bb849e_ku.90sjimg.jpeg", typeId: 7, isCarousel: 0, pv: 0), new Essay(title: "新闻资讯", content: "<p>你好世界</p>", coverUrl: "https://fjut-tiny-spark-1311549442.cos.ap-guangzhou.myqcloud.com/image/f104dd3e-9b81-424e-9999-949f85bb849e_ku.90sjimg.jpeg", typeId: 7, isCarousel: 0, pv: 0)] || HttpStatus.SUCCESS | 2
    }

    @Unroll
    def "setCarousel(essayIds:#essayIds), code:#httpStatus, msg:#resultMsg"() {
        given: "设置请求参数"
        essayMapper.setCarousel(essayIds) >> 1;

        when:
        def response = essayServiceImp.setCarousel(essayIds);

        then:
        with(response) {
            code == httpStatus;
            msg == resultMsg
        }

        where:
        essayIds    || httpStatus | resultMsg
        1  || HttpStatus.SUCCESS | "设置成功"
    }

    @Unroll
    def "getCarouselEssays(), code:#httpStatus"() {
        given: "设置请求参数"
        essayMapper.selectList(_ as QueryWrapper) >> resultSet

        when:
        def response = essayServiceImp.getCarouselEssays();

        then:
        with(response) {
            response.get("code") == httpStatus;
        }

        where:
        resultSet || httpStatus
        [new Essay(title: "新闻资讯", content: "<p>你好世界</p>", coverUrl: "https://fjut-tiny-spark-1311549442.cos.ap-guangzhou.myqcloud.com/image/f104dd3e-9b81-424e-9999-949f85bb849e_ku.90sjimg.jpeg", typeId: 7, isCarousel: 1, pv: 0), new Essay(title: "新闻资讯", content: "<p>你好世界</p>", coverUrl: "https://fjut-tiny-spark-1311549442.cos.ap-guangzhou.myqcloud.com/image/f104dd3e-9b81-424e-9999-949f85bb849e_ku.90sjimg.jpeg", typeId: 7, isCarousel: 1, pv: 0)] || HttpStatus.SUCCESS
    }
}